﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using Bamboo.Core.Data;
using System.Collections;
namespace TinyERP.Framework.Controls
{
    public delegate void CustomDataGridViewSortEventHandler(object sender, SortColumnChangedEvent e);
    public class SortColumnChangedEvent : EventArgs
    {
        public string ColumnName { get; set; }
        public SortDirection SortDirection { get; set; }
    }
    public class CustomDataGridView : ComponentFactory.Krypton.Toolkit.KryptonDataGridView, ILocalizable
    {
        public string ModuleAssemblyName { get; set; }
        public event CustomDataGridViewSortEventHandler ColumnSortChanged;
        public Hashtable SortKeyMap { get; set; }
        public bool SortOnCurrentPage { get; set; }
        public CustomDataGridView()
            : base()
        {
            this.ColumnHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.OnDataGridColumnHeaderClick);
            SortKeyMap = new Hashtable();
            this.AutoGenerateColumns = false;
        }

        private void OnDataGridColumnHeaderClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewColumn newColumn = this.Columns[e.ColumnIndex];
            DataGridViewColumn oldColumn = this.SortedColumn;
            if (newColumn == null || newColumn.SortMode == DataGridViewColumnSortMode.NotSortable)
            {
                return;
            }
            ListSortDirection direction = GetSortDirection(newColumn, oldColumn);
            
            SortColumn(newColumn, direction);

            SwitchSortMode(newColumn, direction);
        }
        private ListSortDirection GetSortDirection(DataGridViewColumn newColumn, DataGridViewColumn oldColumn)
        {
            ListSortDirection direction = ListSortDirection.Ascending;
            if (oldColumn != null)
            {
                if (oldColumn == newColumn)
                {
                    direction = (this.SortOrder == SortOrder.Ascending) ? ListSortDirection.Descending : ListSortDirection.Ascending;
                }
                else
                {
                    oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
                }
            }
            return direction;
        }

        private void SortColumn(DataGridViewColumn column, ListSortDirection direction)
        {
            if (!SortOnCurrentPage)
            {
                SortColumnChangedEvent sortEvent = new SortColumnChangedEvent();
                string sortKey = (string) SortKeyMap[column.DataPropertyName];
                if (sortKey == null)
                {
                    sortKey = column.DataPropertyName;
                }
                sortEvent.ColumnName = sortKey;
                sortEvent.SortDirection = (direction == ListSortDirection.Ascending) ? SortDirection.ASCENDING : SortDirection.DESCENDING;
                if (ColumnSortChanged != null)
                {
                    ColumnSortChanged(this, sortEvent);
                }
            }
            this.Sort(column, direction);
        }
        private void SwitchSortMode(DataGridViewColumn column, ListSortDirection direction)
        {
            if (column.HeaderCell.SortGlyphDirection == SortOrder.None)
            {
                column.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
            }
            else
            {
                SortOrder switchDirection = (direction == ListSortDirection.Ascending) ?
                    SortOrder.Ascending : SortOrder.Descending;
                column.HeaderCell.SortGlyphDirection = switchDirection;
            }
        }

        #region ILocalizable Members

        public void UpdateLanguage()
        {
            foreach (DataGridViewColumn column in this.Columns)
            {
                string resourceKey = column.DataPropertyName;
                if (!string.IsNullOrEmpty(this.ModuleAssemblyName))
                {
                    column.HeaderText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey(resourceKey +"," + ModuleAssemblyName);
                }
                else
                {
                    column.HeaderText = AppResourceManager.GetInstance().GetString(resourceKey);
                }
            }
        }

        #endregion
    }
}
